package cn.ticket.controller;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;

import org.apache.http.protocol.HTTP;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;

import cn.ticket.model.Relatives;
import cn.ticket.model.Staff;
import cn.ticket.service.RelativesService;
import cn.ticket.service.StaffService;
import cn.ticket.service.Utility;

@Controller
@RequestMapping("relatives")
public class RelativesController {
@Autowired
RelativesService relativesService;
@Autowired
StaffService staffService;
@RequestMapping(value="insert",method=RequestMethod.POST,produces={"application/json;charset=utf-8"})
@ResponseBody
public String insert(@RequestBody Relatives record){
	Map<String, Object> result=new HashMap<>();
	Integer staffId=record.getStaffid();
	Staff staff=staffService.selectByPrimaryKey(staffId);
	//外聘职工不许添加亲属
	if (staff.getStatus().equals("2")) {
		result.put("status", 1002);
		result.put("message", "外聘员工暂时不能添加亲属！");
		return Utility.gson.toJson(result);
	}
	//已退休职工仅能添加一个亲属（配偶）
	if (staff.getStatus().equals("4")) {
		List<Relatives> relatives=relativesService.getByStaffId(staffId);
		if (!relatives.isEmpty()) {
			result.put("status", 1002);
			result.put("message", "已退休职工仅能添加一个亲属(配偶)!");
			return Utility.gson.toJson(result);
		}
	}
	int num=relativesService.insertSelective(record);
	if (num==1) {
		result.put("status", 1001);
	}
	else {
		result.put("message", "添加失败(亲属不能重复添加)!");
		result.put("status", 1002);
	}
	return Utility.gson.toJson(result);
};
@RequestMapping(value="update",method=RequestMethod.POST,produces={"application/json;charset=utf-8"})
@ResponseBody
public String update(@RequestBody Relatives record){
	Map<String, Object> result=new HashMap<>();
	int num=relativesService.updateByPrimaryKeySelective(record);
	if (num==1) {
		result.put("status", 1001);
	}
	else {
		result.put("status", 1002);
	}
	return Utility.gson.toJson(result);
};
@RequestMapping(value="delete",method=RequestMethod.POST,produces={"application/json;charset=utf-8"})
@ResponseBody
public String delete(@RequestBody Map<String, String> args){
	Map<String, Object> result=new HashMap<>();
	int id=Integer.parseInt(args.get("id"));

	Relatives relative=relativesService.selectByPrimaryKey(id);	
	if (args.get("staffId")!=null) {
		int staffId=Integer.parseInt(args.get("staffId"));
		if (relative.getStaffid()!=staffId) {
			result.put("status", 1002);
			result.put("message", "错误,此亲属不属于你的Id!有问题请联系管理员!");
			return Utility.gson.toJson(result);
		}
	}
	if (relative.getTicketcount()!=0) {
		result.put("status", 1002);
		result.put("message", "已有乘坐次数的亲属不许删除,如有需要请联系所办或管理员!");
		return Utility.gson.toJson(result);
	}
	int num=relativesService.deleteByPrimaryKey(id);
	if (num==1) {
		result.put("status", 1001);
	}
	else {
		result.put("status", 1002);
	}
	return Utility.gson.toJson(result);
};
@RequestMapping(value="getAll",produces={"application/json;charset=utf-8"})
@ResponseBody
public String getAll(){
	Map<String, Object> result=new HashMap<>();
	
	List<Relatives> relatives=relativesService.getAll();
	if (!relatives.isEmpty()) {
		result.put("status", 1001);
		result.put("body", relatives);
	}
	else {
		result.put("status", 1002);
	}
	return Utility.gson.toJson(result);
};
@RequestMapping(value="getByStaffId",method=RequestMethod.POST,produces={"application/json;charset=utf-8"})
@ResponseBody
public String getByStaffId(@RequestBody Map<String, String> args){
	Map<String, Object> result=new HashMap<>();
	int staffId=Integer.parseInt(args.get("staffId"));
	List<Relatives> relatives=relativesService.getByStaffId(staffId);
	Staff staff=staffService.selectByPrimaryKey(staffId);
	//如果职工已退休，则只能显示一个亲属
	if (staff.getStatus().equals("4")&&!relatives.isEmpty()) {
		Relatives firstRelative=relatives.get(0);
		relatives.clear();
		relatives.add(firstRelative);
	}
	if (!relatives.isEmpty()) {
		result.put("status", 1001);
		result.put("body", relatives);
	}
	else {
		result.put("status", 1002);
	}
	return Utility.gson.toJson(result);
};
}
